package com.sys.service;

import com.sys.GlobalVariable;
import com.sys.entity.User;
import com.sys.exception.DuplicateNameException;
import com.sys.exception.LoginException;
import com.sys.utils.DbUtil4;
import com.sys.utils.DbUtil4Factory;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserService {
    public static int modify(String newPwd,int uid) {

        DbUtil4 db = null;
        String sql = "UPDATE t_user SET upassword = ? where uid = ?";
        try {
            db = DbUtil4Factory.getDbUtil4("db.config", "utf-8");
            int i = db.update(sql, new Object[]{newPwd, uid});
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return 0;
    }

    public User login(String username, String password) throws Exception {
        DbUtil4 db = null;
        String sql = "select uid,username,upassword,create_time,head from t_user where username = ? and upassword = ?";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            List<User> list = db.query(sql, new Object[]{username, password}, User.class);
            if (list != null && !list.isEmpty()) {
                User user = list.get(0);
                return user;
            }else{
                throw new LoginException("用户名不存在，或密码错误！");
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void regist(User user) throws Exception {
        // 判断数据库是否有该用户名，如果有就抛异常，用户已经存在（有多种实现方式）
        // 不存在就添加到 t_user 表中
        DbUtil4 db = null;
        String sql = "select count(1) from t_user where username = ? ";
        String insertSQL = "insert into t_user(username,upassword,head,create_time) values (?,?,?,?)";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            List<Map<String,Object>> list = db.query(sql, new Object[]{user.getUsername()});
            // 执行的是 select count(1)，要么是0，要么是1，一定有一个结果
            Map<String, Object> map = list.get(0);
            Long count = (Long)map.get("count(1)");
            if(count == 0){ // 不存在同名用户
                int num = db.update(insertSQL,new Object[]{user.getUsername(),user.getUpassword(),user.getHead(),new Date()});
            }else{// 存在同名用户就抛异常
                throw new DuplicateNameException("用户名已经存在，请重新注册！");
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }

    }
}
